#include <cstdlib>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string.h>
#include <stdio.h>

int exe_query(MYSQL *pconn,const char * sql);

int main()
{
    //初始化
    MYSQL conn;
    MYSQL * pconn = mysql_init(&conn);
    //连接到数据库
    const char * host = "localhost";
    const char * user = "root";
    const char * passwd = "Root123!";
    const char * db = "april_db";
    pconn = mysql_real_connect(&conn,host,user,passwd,db,0,NULL,0);
    if(!pconn){
        fprintf(stderr,"%s\n",mysql_error(&conn));
        return EXIT_FAILURE;
    }
    printf("mysql connect success!\n");
    
    //执行查询
    const char * sql = "INSERT INTO Student(s_name,s_birth,s_sex) "
            "VALUES('熊大','2000-01-01','男')";
    exe_query(pconn,sql);
   
    //查询2
    const char * sql2 = "UPDATE Student SET s_name='熊二' WHERE s_id=3";
    exe_query(pconn,sql2);

    const char * sql3 = "DELETE FROM Student WHERE s_id = 11";
    exe_query(pconn,sql3);
    


    mysql_close(pconn);


    return 0;
}


int exe_query(MYSQL *pconn,const char * sql){
    int ret = mysql_real_query(pconn,sql,strlen(sql));
    if(ret != 0){
        fprintf(stderr,"ERROR %d : %s\n",
                mysql_errno(pconn),mysql_error(pconn));
        return EXIT_FAILURE;
    }
    printf("QUERY OK, %ld row affected,\n",mysql_affected_rows(pconn));
    return 0;

}
